package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.result.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.impl.EmployeeServiceImpl;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IEmployeeService employeeService;


    @RequiresPermissions(value = {"customer:list", "潜在客户页面"}, logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        // 判断当前登入用户是否包含admin或者经理角色,如果有可以查询所有数据
        Subject subject = SecurityUtils.getSubject();
        
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager "))) {
            // 如果没有就只能查询自己的数据
            qo.setSellerId(UserContext.getEmployee().getId());
        }

        // 只查询潜在客户状态的数据
        qo.setStatus(Customer.STATUS_COMMON);

        // 查询职业下拉框的数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs",jobs);
        // 查询来源下拉框的数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources",sources);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        // 查询带有Market和Market_Manager的员工
        List<Employee> sellers = employeeService.selectBySn("Market","Market_Manager");
        model.addAttribute("sellers",sellers);

        // 交流方式下拉框结果集
        List<SystemDictionaryItem> communicativeMeans = systemDictionaryItemService.selectByParentSn(
                "communicationMethod");
        model.addAttribute("communicativeMeans",communicativeMeans);

        // 查询销售人员,作为潜行客户移交时新销售人员的下拉框数据
        List<Employee> employees = employeeService.selectBySn("Market");
        model.addAttribute("newSeller",employees);
        return "customer/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete", "潜在客户删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult(true,"删除成功");
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate", "潜在客户新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {
        if (customer.getId() != null) {
            customerService.update(customer);
        } else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
